package midway.madness.post.article;

import java.util.List;

import midway.madness.R;
import midway.madness.post.PostObject;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ArticlesListAdapter extends ArrayAdapter<PostObject>{

	private List<PostObject> postObjs;

	public ArticlesListAdapter(Context context,	int textViewResourceId, List<PostObject> objects) {
		super(context, textViewResourceId, objects);
		postObjs = objects;
	}
	
	public View getView(int position, View convertView, ViewGroup parent){
		View retView = convertView;
		if (null == retView){
			LayoutInflater lf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			retView = lf.inflate(R.layout.article_info_view, null);
		}
		PostObject post = postObjs.get(position);
		if (null != post){
			TextView title = (TextView) retView.findViewById(R.id.list_title_text);
			TextView author = (TextView) retView.findViewById(R.id.list_author_text);
			
			if (null != title){
				title.setText(Html.fromHtml(post.getTitle()));
			}
			if (null != author){
				author.setText(post.getDate() + " BY " + post.getAuthor().toUpperCase());
			}
		}
		return retView;
	}
	
}
